<html>
<head><meta charset="utf-8"><title>unreachable canoncalization · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html">unreachable canoncalization</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="180734451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180734451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180734451">(Nov 14 2019 at 14:19)</a>:</h4>
<p>So there's now two proposed MIR optimizations (<a href="https://github.com/rust-lang/rust/issues/66282" target="_blank" title="https://github.com/rust-lang/rust/issues/66282">#66282</a>, <a href="https://github.com/rust-lang/rust/issues/66329" target="_blank" title="https://github.com/rust-lang/rust/issues/66329">#66329</a>) that try to exploit "executing unreachable is UB" and need to take care that the <code>Unreachable</code> they're looking at is actually guaranteed to execute. I could imagine there's more in the future. Naively, this could require scanning lots of basic blocks.</p>
<p>It seems like, perhaps, there should be a clean-up which runs once in a while and which other passes rely on for quality of optimizations (not for correctness ofc) rather than redoing analyses. Similar to CFG simplification, if you will. In fact, maybe <a href="https://github.com/rust-lang/rust/issues/66329" target="_blank" title="https://github.com/rust-lang/rust/issues/66329">#66329</a> could be that pass.</p>
<p>Alternatively or in addition, as I sketched in <a href="https://github.com/rust-lang/rust/pull/66282#discussion_r346319446" target="_blank" title="https://github.com/rust-lang/rust/pull/66282#discussion_r346319446">https://github.com/rust-lang/rust/pull/66282#discussion_r346319446</a>, we could trim <code>s1; s2; ...; sN; unreachable</code> to <code>s1; s2; ...; sK; unreachable</code> where <code>sK</code> is the last statement (if any) that can diverge (and thus, make the basic block not-UB). This could be very cheap in the case where nothing changes (only need to look at the last statement in the BB) so we could perhaps run it more frequently. And after this trimming, it's easy to identify basic blocks which will surely cause UB if jumped to (since their statement list is empty).</p>
<p>What do y'all think? cc <span class="user-mention" data-user-id="124288">@oli</span>  <span class="user-mention" data-user-id="126931">@centril</span></p>



<a name="180735216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180735216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180735216">(Nov 14 2019 at 14:27)</a>:</h4>
<p>I like it, this seems like a much more modular (and thus maintainable) approach</p>



<a name="180735285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180735285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180735285">(Nov 14 2019 at 14:28)</a>:</h4>
<p>and it automatically errors on the conservative side instead of silently doing bad optimizations</p>



<a name="180735855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180735855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180735855">(Nov 14 2019 at 14:35)</a>:</h4>
<p>Seems good</p>



<a name="180735871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180735871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180735871">(Nov 14 2019 at 14:36)</a>:</h4>
<p>My one question is how we roll this out since we have two separate PRs now</p>



<a name="180735939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180735939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180735939">(Nov 14 2019 at 14:36)</a>:</h4>
<p>should we land both and then do cleanup after or do it from the get-go?</p>



<a name="180736295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736295">(Nov 14 2019 at 14:40)</a>:</h4>
<p>I don't have a strong opinion on that.</p>



<a name="180736363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736363">(Nov 14 2019 at 14:41)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> please instruct :P</p>



<a name="180736465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736465">(Nov 14 2019 at 14:42)</a>:</h4>
<p>I think we can just make the current PRs mir-opt-level=3</p>



<a name="180736492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736492">(Nov 14 2019 at 14:42)</a>:</h4>
<p>then we can merge them (+ have an open issue about the unsoundness of the optimizations)</p>



<a name="180736526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736526">(Nov 14 2019 at 14:43)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> wouldn't be too difficult to filter out bbs with <code>InlineAsm</code>  in my PR</p>



<a name="180736533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736533">(Nov 14 2019 at 14:43)</a>:</h4>
<p>so I can make it sound from the start</p>



<a name="180736559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736559">(Nov 14 2019 at 14:43)</a>:</h4>
<p><span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span> as long as it has a FIXME with an issue</p>



<a name="180736609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736609">(Nov 14 2019 at 14:44)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="180736634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736634">(Nov 14 2019 at 14:44)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> what are you thoughts on inlining and my optimization?</p>



<a name="180736653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180736653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180736653">(Nov 14 2019 at 14:44)</a>:</h4>
<p>(also, why aren't we inlining atm on release mode?)</p>



<a name="180739040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180739040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180739040">(Nov 14 2019 at 15:09)</a>:</h4>
<p>inlining is still broken</p>



<a name="180739046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180739046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180739046">(Nov 14 2019 at 15:09)</a>:</h4>
<p>you can't boostrap rustc</p>



<a name="180739059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180739059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180739059">(Nov 14 2019 at 15:09)</a>:</h4>
<p>but I don't have the capacity to tackle another project atm</p>



<a name="180739075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180739075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180739075">(Nov 14 2019 at 15:09)</a>:</h4>
<p>and I don't even have any tips to give someone who'd want to tackle it</p>



<a name="180739531"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180739531" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180739531">(Nov 14 2019 at 15:14)</a>:</h4>
<p>ouch; then my optimization should definitely be -Z mir-opt-level=3</p>



<a name="180740425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/unreachable%20canoncalization/near/180740425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/unreachable.20canoncalization.html#180740425">(Nov 14 2019 at 15:22)</a>:</h4>
<blockquote>
<p>and I don't even have any tips to give someone who'd want to tackle it</p>
</blockquote>
<p>I have been looking at this off and on but I'm currently sidetracked on const prop stuff. My previous approach was doing binary searches with the <code>-Z fuel</code> argument to find where the mis-optimization was occurring. But that's very time intensive...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>